//
//  HomeMoneyController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/8/21.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "HomeMoneyController.h"
#import "HomeBodyTableViewCell.h"
#import "SelectController.h"
#import "ReSetController.h"
@interface HomeMoneyController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSArray * infoArray;
@property(nonatomic,strong)NSMutableDictionary * dict;
@end

@implementation HomeMoneyController
{
    NSString * _hkm;
    NSString * _sfdbm;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     _infoArray = @[@"户口性质",@"户籍所在地",@"家庭居住地址",@"家庭总人口",@"家庭年收入",@"主要收入来源",@"联系电话",@"邮编",@"是否低保",@"年均人收入"];
     [self requestM];
    [self createTable];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 30, 20) target:self sel:@selector(baoClick)];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RXQHKLBMC:) name:@"RXQHKLBMC" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HKSZD:) name:@"HKSZD" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JTZZ:) name:@"JTZZ" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JTRK:) name:@"JTRK" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JTNSR:) name:@"JTNSR" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JTZYSRLY:) name:@"JTZYSRLY" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JTDH:) name:@"JTDH" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JTYZBM:) name:@"JTYZBM" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NJRSR:) name:@"NJRSR" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SFDB:) name:@"SFDB" object:nil];
    
}

-(void)createTable
{
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.showsHorizontalScrollIndicator=NO;
    _tableView.userInteractionEnabled= YES ;
    
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, -5, 0);
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
}

#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.infoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    HomeBodyTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeBodyTableViewCell" owner:nil options:nil] lastObject];
    cell.titleLab.text = self.infoArray[indexPath.row];
    if(indexPath.row==0){
        cell.nameLab.text = _dict[@"RXQHKLBMC"];
    }else if (indexPath.row==1){
        cell.nameLab.text = _dict[@"HKSZD"];
    }else if (indexPath.row==2){
        cell.nameLab.text = _dict[@"JTZZ"];
    }else if (indexPath.row==3){
        long text = [_dict[@"JTRK"] longValue];
        cell.nameLab.text =[NSString stringWithFormat:@"%ld",text];
    }else if (indexPath.row==4){
        cell.nameLab.text =[NSString stringWithFormat:@"%ld",[_dict[@"JTNSR"] longValue]] ;
    }else if (indexPath.row==5){
        cell.nameLab.text = _dict[@"JTZYSRLY"];
    }else if (indexPath.row==6){
        cell.nameLab.text = _dict[@"JTDH"];
    }else if (indexPath.row==7){
        cell.nameLab.text = _dict[@"JTYZBM"];
    }else if (indexPath.row==8){
        if ([_sfdbm isEqualToString:@"0"]) {
             cell.nameLab.text = @"否";
        }else{
        cell.nameLab.text = @"是";
        }
       
    }else if (indexPath.row==9){
        long text = [_dict[@"NJRSR"] longValue];
        cell.nameLab.text =[NSString stringWithFormat:@"%ld",text];
        cell.btn.hidden = YES;
    }

    return cell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 43;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"RXQHKLBMC";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row==8){
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"SFDB";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==9){
    
    }
    else{
        NSArray * array = @[@"RXQHKLBMC",@"HKSZD",@"JTZZ",@"JTRK",@"JTNSR",@"JTZYSRLY",@"JTDH",@"JTYZBM",@"SFDB",@"NJRSR"];
        ReSetController *vc = [[ReSetController alloc] init];
        vc.reStr = array[indexPath.row];
        if (indexPath.row==1) {
             vc.restr = _dict[@"HKSZD"];
        }else if (indexPath.row==2){
        vc.restr = _dict[@"JTZZ"];
        }else if (indexPath.row==3){
            long text = [_dict[@"JTRK"] longValue];
            vc.restr = [NSString stringWithFormat:@"%ld",text];
        }else if (indexPath.row==4){
            vc.restr =[NSString stringWithFormat:@"%ld",[_dict[@"JTNSR"] longValue]];
        }else if (indexPath.row==5){
            vc.restr = _dict[@"JTZYSRLY"];
        }else if (indexPath.row==6){
            vc.restr = _dict[@"JTDH"];
        }else if (indexPath.row==7){
            vc.restr = _dict[@"JTYZBM"];
        }
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)RXQHKLBMC:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"RXQHKLBMC"] = sender.userInfo[@"hk"];
    _hkm = sender.userInfo[@"hkm"];
    [self.tableView reloadData];
}
-(void)HKSZD:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"HKSZD"] = sender.userInfo[@"hkszd"];
    [self.tableView reloadData];
}
-(void)JTZZ:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"JTZZ"] = sender.userInfo[@"jtzz"];
    [self.tableView reloadData];
}
-(void)JTRK:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"JTRK"] = sender.userInfo[@"jtrk"];
    float rj = [_dict[@"JTNSR"] intValue]/[_dict[@"JTRK"] intValue];
    _dict[@"NJRSR"] = [NSString stringWithFormat:@"%f",rj];
    [self.tableView reloadData];
}
-(void)JTNSR:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"JTNSR"] = sender.userInfo[@"jtnsr"];
    float rj = [_dict[@"JTNSR"] intValue]/[_dict[@"JTRK"] intValue];
    _dict[@"NJRSR"] = [NSString stringWithFormat:@"%f",rj];
    [self.tableView reloadData];
}
-(void)JTZYSRLY:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"JTZYSRLY"] = sender.userInfo[@"jtzysrly"];
    [self.tableView reloadData];
}
-(void)JTDH:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"JTDH"] = sender.userInfo[@"jtdh"];
    [self.tableView reloadData];
}
-(void)JTYZBM:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"JTYZBM"] = sender.userInfo[@"jtyzbm"];
    [self.tableView reloadData];
}
-(void)NJRSR:(NSNotification *)sender{
    //打印通知传过来的数值
    _dict[@"NJRSR"] = sender.userInfo[@"njrsr"];
    [self.tableView reloadData];
}
-(void)SFDB:(NSNotification *)sender{
    //打印通知传过来的数值
    _sfdbm = sender.userInfo[@"sfdbm"];
    [self.tableView reloadData];
}

-(void)requestM
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid]};
    [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/getHomeIncome.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"message"]isEqualToString:@"成功"]) {
            _dict = [[NSMutableDictionary alloc]initWithDictionary:object[@"homeIncomeList"]];
            _hkm = _dict[@"RXQHKLBM"];
            
            _sfdbm =[NSString stringWithFormat:@"%d",[_dict[@"SFDB"] intValue]];
            [_tableView reloadData];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}

-(void)baoClick
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid],
                             @"JTRK":_dict[@"JTRK"],
                             @"HKSZD":_dict[@"HKSZD"],
                             @"JTZYSRLY":_dict[@"JTZYSRLY"],
                             @"JTYZBM":_dict[@"JTYZBM"],
                             @"SFDB":_sfdbm,
                             @"RXQHKLBM":_hkm,
                             @"JTDH":_dict[@"JTDH"],
                             @"JTZZ":_dict[@"JTZZ"],
                             @"NJRSR":[NSString stringWithFormat:@"%ld",[_dict[@"NJRSR"] longValue]],
                             @"JTNSR":[NSString stringWithFormat:@"%ld",[_dict[@"JTNSR"] longValue] ],
                            };
    [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/updateHomeIncome.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
